<template>
  <div class="upload-warp">
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
      @mouseenter.native="coverShow = imageUrl ? true : false"
      @mouseleave.native="coverShow = false"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <div class="cover" v-if="coverShow">
        <el-icon
          class="el-icon-zoom-in"
          @click.native.stop="showDialog = true"
        ></el-icon>
        <el-icon
          @click.native.stop="doclick"
          class="el-icon-delete-solid"
        ></el-icon>
      </div>
    </el-upload>
    <el-dialog title="预览图片" :visible.sync="showDialog">
      <img
        style="width:100%;display:block;margin:0 auto"
        :src="imageUrl"
        alt=""
      />
    </el-dialog>
    <el-dialog title="预览图片" :visible.sync="showCode">
      <img
        style="width:100%;display:block;margin:0 auto"
        :src="imageUrl"
        alt=""
      />
    </el-dialog>
  </div>
</template>
<script>
// 导入
import COS from 'cos-js-sdk-v5'
// 实例化一个腾讯云的操作对象
let cos = new COS({
  SecretId: 'AKIDoVyHMDvRkqhJvMYWQBi3A2e2B9vTmrhI',
  SecretKey: 'PbbOxHPK2VjkX6QVN0OVMzRkrmRs9HH5'
})
export default {
  data () {
    return {
      imageUrl: '',
      coverShow: false,
      showDialog: false,
    }
  },
  methods: {
    doclick () {
      this.imageUrl = ''
      this.coverShow = false
    },
    upload (params) {
      cos.putObject(
        {
          Bucket: 'hr72-1306809259' /* 必须 名字*/,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: params.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: params.file, // 上传文件对象

          // 回调函数,上传进度的回调函数
          onProgress: progressData => {
            // console.log(JSON.stringify(progressData))
            this.percent = progressData.percent * 100
          }
        },
        (err, data) => {
          // console.log(err || data)
          // console.log(data);
          if (!err) {
            // console.log(data.Location);
            this.imageUrl = 'http://' + data.Location
          }
        }
      )
    },
    beforeAvatarUpload (file) {
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="scss">
.cover {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  i {
    font-size: 30px;
    margin-right: 10px;
    color: greenyellow;
  }
}
.avatar-uploader {
  height: 178px;
  width: 178px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
